package com.ketai.appinfo.aop;/*package com.ketai.aop;

*//**
 * @Author:帅帅的刘
 * @Description: 日志的切面类
 * @Date:Create In 2020-10-31 15:23
 **//*

import com.ketai.controller.SysLogController;
import com.ketai.pojo.SmbmsUser;
import com.ketai.pojo.Syslog;
import com.ketai.service.SyslogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

// 切面类
@Aspect
@Component
public class LogAop {
    @Resource
    private HttpServletRequest req;

    @Resource
    private SyslogService syslogService;

    private Date startTime;  // 开始时间
    private Class executionClass;  // 访问的真实目标类
    private String methodName; // 访问的方法

    // 增强处理
    @Before("execution(* com.ketai.service.*.*(..))")
    public void doBefore(JoinPoint jp) throws NoSuchMethodException, SecurityException {
        startTime = new Date(); // 访问时间
        // 获取访问的类
        executionClass = jp.getTarget().getClass();
        // 获取访问的方法
         methodName = jp.getSignature().getName();// 获取访问的方法的名称

    }


    @After("execution(* com.ketai.controller.*.*(..))")
    public void doAfter(JoinPoint jp) {
        // 获取类上的@RequestMapping对象
        if (executionClass != SysLogController.class) {
            String url = req.getRequestURI();
            // 创建日志类对象
            Syslog sysLog = new Syslog();
            // 获取访问时长
            Long executionTime = System.currentTimeMillis() - startTime.getTime();
            // 将sysLog对象属性封装
            sysLog.setExecutiontime(executionTime);
            sysLog.setUrl(url);
            // 获取ip
            String ip = req.getRemoteAddr();
            sysLog.setIp(ip);
            SmbmsUser user = (SmbmsUser) req.getSession().getAttribute("userSession");
            String username = user.getUserName();
            sysLog.setUsername(username);
            sysLog.setMethod("[类名]" + executionClass.getName() + "[方法名]" +
                    methodName);
            sysLog.setVisittime(startTime);
            // 调用Service，调用dao将sysLog insert数据库
            syslogService.saveLog(sysLog);


        }
    }
}*/
